<?php
/*
 * @Author: 李明
 * @Date: 2021-08-11 19:15:20
 * @LastEditTime: 2022-01-06 13:02:58
 * @LastEditors: 李明
 * @Description: 
 * @FilePath: /CRM2.0-PHP/app/Http/Controllers/system/SystemController.php
 * @山东壹零贰肆信息科技有限公司
 */

namespace App\Http\Controllers\system;

use App\Http\Controllers\common\LMReturn;
use App\Http\Controllers\Controller;
use App\Http\Resources\System;
use App\Models\CustArea;
use App\Models\SystemBase;
use Illuminate\Http\Request;

class SystemController extends Controller
{
    /**
     * @description: 三级联动数据返回
     * @param {Request} $request
     * 
     */
    public function GetAreaInfo(Request $request)
    {
        //接收前端传递过来的值
        $all = $request->all();
        //通过传递过来的值进行判断，如果什么都没有传递的话，那么就返回所有的父类ID ，反之就是传递了什么就返回什么的子集
        if (!array_key_exists("area_parentcode", $all)) {
            //这里是返回省
            $info = json_decode(CustArea::select("area_id", "area_name", "area_code", "area_parentcode")
                ->where('area_parentcode', 000000)
                ->get(), true);
        } else {
            //这里返回的是市信息
            $info = json_decode(CustArea::select("area_id", "area_name", "area_code", "area_parentcode")
                ->where('area_parentcode', $all['area_parentcode'])
                ->get(), true);
        }
        return LMReturn::Success($info);
    }
    /**
     * @description: 系统信息修改
     * @param {Request} $request
     * @return {*}
     */
    public function SystemDetailChange(Request $request)
    {
        try {
            //去验证token值
            $tokenInfo = json_decode($this->verifyTokenLm(), true);
            //判断传递过来的token的code码是否等于400  如果是400的话，提示token不正确
            if ($tokenInfo['code'] == 400) {
                return LMReturn::Error($tokenInfo['msg'], $tokenInfo['code']);
            }
            $all = $request->all();
            $default_password = $all['default_password'];
            $password_type = $all['password_type'];
            $service_tel = $all['service_tel'];
            $technical_support = $all['technical_support'];
        } catch (\Throwable $th) {
            return LMReturn::Error('系统设置错误', 'SC001');
        }
        try {
            $sys = SystemBase::find(1) ?? new SystemBase;
            $sys->id = 1;
            $sys->default_password = $default_password;
            $sys->password_type = $password_type;
            $sys->service_tel = $service_tel;
            $sys->technical_support = $technical_support;
            // $sys->int_rules = $int_rules;
            $sys->save();
            return LMReturn::Success($sys, '修改成功');
        } catch (\Throwable $th) {
            return LMReturn::Error('请联系技术支持', 'SC002');
        }
    }

    /**
     * @description: 系统信息查询
     * @param {Request} $request
     * @return {*}
     */
    public function SystemDetailQuery(Request $request)
    {
        //接收前端传递过来的值
        $sys = SystemBase::where('id', '1')->get();
        $sys = System::collection($sys);
        $sys = $sys[0];
        return LMReturn::Success($sys);
    }
}
